package com.example.vv.model.search.presenter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.Log;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.example.vv.R;
import com.example.vv.adapter.BaseRecyclerViewAdapter;
import com.example.vv.adapter.RecyclerViewHolder;
import com.example.vv.fragmentWork.entity.HotEntity;
import com.example.vv.fragmentWork.entity.SearchEntity;
import com.example.vv.fragmentWork.entity.TopEntity;
import com.example.vv.fragmentWork.mvp.MvpPresenter;
import com.example.vv.fragmentWork.mvp.ViewISNullException;
import com.example.vv.fragmentWork.utlis.LogUtils;
import com.example.vv.model.search.contract.SearchContract;
import com.example.vv.model.search.model.SearchModel;
import com.example.vv.res.net.BaseResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class SearchPresenter extends MvpPresenter<SearchContract.View, SearchModel> implements SearchContract.Presenter {
    private Paint mSearchHistoryPaint;
    private int mPaddingSize;
    private List<String> histories = new ArrayList<>();

    @Override
    public void attach(SearchContract.View view) {
        super.attach(view);
        model = new SearchModel();
    }


    @Override
    public void setSearchData(String values) {
        model.REQUEST_SEARCH("article/query/0/json", values)
                .map(new Function<BaseResponse, SearchEntity>() {
                    @Override
                    public SearchEntity apply(BaseResponse baseResponse) throws Exception {
                        SearchEntity searchEntity = new Gson().fromJson(baseResponse.getData(), SearchEntity.class);
                        LogUtils.Log(searchEntity.getCurPage() + "getdatasearch");
                        return searchEntity;
                    }
                })
                .subscribe(new Consumer<SearchEntity>() {
                    @Override
                    public void accept(SearchEntity searchEntity) throws Exception {
                        view().getSearchData(searchEntity);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtils.Log(throwable.getMessage());
                    }
                });
    }

    @Override
    public void setHotData() {
        model.REQUEST_Hot("hotkey/json")
                .map(new Function<BaseResponse, List<HotEntity>>() {
                    @Override
                    public List<HotEntity> apply(BaseResponse baseResponse) throws Exception {
                        List<HotEntity> hotEntity = new Gson().fromJson(baseResponse.getData(), new TypeToken<List<HotEntity>>() {
                        }
                                .getType());
                        return hotEntity;
                    }
                })
                .subscribe(new Consumer<List<HotEntity>>() {
                    @Override
                    public void accept(List<HotEntity> hotEntity) throws Exception {
                        view().getHotData(hotEntity);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtils.Log(throwable.getMessage());
                    }
                });
    }

    @Override
    public void setHistoryData(String str) {
        if (str.equals("")) {
        } else {
            histories.add(str);
        }
        try {
            view().getHistoryData(histories);
        } catch (ViewISNullException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setHistoryData2(List<String> list) {
        if (list!=null){
            histories = list;
            try {
                view().getHistoryData(histories);
            } catch (ViewISNullException e) {
                e.printStackTrace();
            }
        }else {
            LogUtils.Log("数据库list为null");
        }
    }

    public void removeAll() {
        histories.clear();
    }

    @Override
    public BaseRecyclerViewAdapter setHOtAdapter(List<HotEntity> list, Activity activity) {
        BaseRecyclerViewAdapter<HotEntity> adapter = new BaseRecyclerViewAdapter<HotEntity>
                (activity, list, R.layout.item_search3) {
            @Override
            protected void onBindData(RecyclerViewHolder holder, HotEntity bean, int position) {
                TextView view = (TextView) holder.getView(R.id.t1);
                if (position < 3) {
                    view.setText((position + 1) + "");
                    view.setTextColor(activity.getResources().getColor(R.color.c_d43d3d));
                    view.setTypeface(Typeface.DEFAULT_BOLD);
                } else {
                    view.setText((position + 1) + "");
                    view.setTextColor(activity.getResources().getColor(R.color.c_ffb036));
                    view.setTypeface(Typeface.DEFAULT);
                }
                TextView view2 = (TextView) holder.getView(R.id.t2);
                view2.setText(bean.getName() + "");
            }
        };
        return adapter;
    }

    @Override
    public BaseRecyclerViewAdapter setSearchAdapter(List<SearchEntity.DatasBean> list, Activity activity) {
        BaseRecyclerViewAdapter<SearchEntity.DatasBean> adapter = new BaseRecyclerViewAdapter<SearchEntity.DatasBean>
                (activity, list, R.layout.item_search) {
            private TextView home_bottom_type1;
            private TextView home_bottom_type;
            private TextView home_content;
            private TextView home_time;
            private TextView home_author;

            @Override
            protected void onBindData(RecyclerViewHolder holder, SearchEntity.DatasBean bean, int position) {
                home_author = (TextView) holder.getView(R.id.home_author);
                home_bottom_type = (TextView) holder.getView(R.id.home_bottom_type);
                home_bottom_type1 = (TextView) holder.getView(R.id.home_bottom_type1);
                home_content = (TextView) holder.getView(R.id.home_content);
                home_time = (TextView) holder.getView(R.id.home_time);
                home_author.setText(bean.getAuthor().equals("")?bean.getShareUser():bean.getAuthor() + "");
                home_bottom_type.setText(bean.getChapterName());
                home_bottom_type1.setText(bean.getSuperChapterName());
                home_content.setText(bean.getTitle());
                home_time.setText(new SimpleDateFormat("MM-dd").format(bean.getPublishTime()) + "");
            }
        };

        return adapter;
    }

    @Override
    public BaseRecyclerViewAdapter setHistoryAdapter(List<String> list, Activity activity) {
        BaseRecyclerViewAdapter<String> adapter = new BaseRecyclerViewAdapter<String>
                (activity, list, R.layout.item_search2) {
            private TextView t1;

            @Override
            protected void onBindData(RecyclerViewHolder holder, String bean, int position) {
                t1 = (TextView) holder.getView(R.id.t1);
                t1.setText(bean);
            }
        };

        return adapter;
    }
}
